<template>
	<view>
		<tn-nav-bar :isBack="true">账号登录</tn-nav-bar>
		<view class="tn-margin-top-sm tn-margin-left-sm tn-margin-right-sm"
			:style="{paddingTop: vuex_custom_bar_height + 20 + 'px'}">
			<tn-form :model="form" ref="form" :errorType="['message']">
				<tn-form-item label="登录账号" prop="account" required="true" :borderBottom="false" labelWidth="150">
					<tn-input v-model="form.account" :customStyle="{border: '2rpx solid #DEDEDE', borderRadius: '10rpx'}" />
				</tn-form-item>
				<tn-form-item label="登录密码" prop="password" required="true" :borderBottom="false"  labelWidth="150">
					<tn-input v-model="form.password" type="safe-password	" :customStyle="{border: '2rpx solid #DEDEDE', borderRadius: '10rpx'}" />
				</tn-form-item>
			</tn-form>
		</view>
		<view class="login__wrapper">
			<view class="login__info tn-flex tn-flex-direction-column tn-flex-col-center tn-flex-row-center">
				<view @click="tn('/subpages/user/register')">还没有账号，去注册</view>
				<view class="login__info__item__button" hover-class="tn-hover" :hover-stay-time="150" @click="userAccountLogin">账号登录
				</view>
			</view>
		</view>
		
		<!-- 微信隐私鉴权保护弹窗开始 -->
		<!-- #ifdef MP-WEIXIN -->
		<privacy-popup ref="privacyComponent"></privacy-popup>
		<!-- #endif -->
		<!-- 微信隐私鉴权弹窗保护结束 -->
		
	</view>
</template>

<script>
	import { userLogin,userAccountLogin } from '@/utils/api/user'
	import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue'
	export default {
		data() {
			return {
				form: {
					account: '',
					password: '',
				},
			}
		},
		onShow() {
			this.userLogin()
		},
		methods: {
			userAccountLogin() {
				let _that = this
				userAccountLogin(this.form).then(res => {
					if (res.code == 100) {
						this.$func.showToast(res.msg)
						setTimeout(function() {
							_that.navBack()
						}, 2000)
						return
					}
					_that.$func.showToast(res.msg)
				})
			},
			userLogin() {
				let _that = this
				let sysInfo = {}
				uni.getSystemInfo({
					success(res) {
						sysInfo = res
					}
				})
				uni.login({
					provider: 'weixin',
					success(res) {
						userLogin({
							code: res.code,
							sysInfo: sysInfo
						}).then(res => {
							if (res.code == 100) {
								uni.setStorageSync("login", res.data.token)
							}
						})
					},
					fail(res) {
						_that.$func.showToast(res.errMsg)
					}
				})
			},
			// 返回上一页
			navBack() {
				const pages = getCurrentPages()
				if (pages && pages.length > 0) {
					const firstPage = pages[0]
					if (pages.length == 1 && (!firstPage.route || firstPage.route != 'pages/index/index')) {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					} else {
						uni.navigateBack({
							delta: 1
						})
					}
				} else {
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}
			},
			tn(e) {
				this.$func.navigatorTo(e)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.login {
		position: relative;
		height: 100%;
		z-index: 1;

		/* 背景图片 start */
		&__bg {
			z-index: -1;
			position: fixed;

			&--top {
				top: 0;
				left: 0;
				right: 0;
				width: 100%;

				.bg {
					width: 750rpx;
					will-change: transform;
				}

				.rocket {
					margin: 50rpx 28%;
					width: 400rpx;
					will-change: transform;
				}
			}

			&--bottom {
				bottom: -10rpx;
				left: 0;
				right: 0;
				width: 100%;
				// height: 144px;
				margin-bottom: env(safe-area-inset-bottom);

				image {
					width: 750rpx;
					will-change: transform;
				}
			}
		}

		/* 背景图片 end */

		/* 内容 start */
		&__wrapper {
			width: 100%;
		}


		&__info {
			// margin: 0 30rpx;
			// margin-top: 105rpx;
			padding: 10rpx 51rpx 30rpx 51rpx;
			padding-bottom: 0;
			border-radius: 20rpx;
			background-color: #ffff;
			box-shadow: 0rpx 0rpx 0rpx 0rpx rgba(0, 3, 72, 0.1);

			&__item {
				&__button {
					margin-top: 75rpx;
					margin-bottom: 39rpx;
					width: 100%;
					height: 77rpx;
					text-align: center;
					font-size: 31rpx;
					font-weight: bold;
					line-height: 77rpx;
					letter-spacing: 1em;
					text-indent: 1em;
					border-radius: 39rpx;
					color: #fff;
					background-color: #00C3FF;
					box-shadow: 1rpx 10rpx 24rpx 0rpx rgba(60, 129, 254, 0.35);
				}

				&__tips {
					margin: 10rpx 0;
					color: #AAAAAA;
				}
			}
		}

	}
</style>